package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;
    @GetMapping
    public Result list( UserDto userDto){
        PageResult<User> pageResult = userService.list(userDto);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result add(@RequestBody User user){
        userService.add(user);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids){
        log.info("ids:{}", ids);
        userService.deleteById(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        User user = userService.getById(id);
        return Result.success(user);
    }

    @PutMapping()
    public Result update(@RequestBody User user){
        userService.update(user);
        return Result.success();
    }
    @GetMapping("/list")
    public Result listAll(){
        List<User> users = userService.listAll();
        return Result.success(users);
    }

    @GetMapping("/role/{roleLabel}")
    public Result userByRole(@PathVariable String roleLabel){
        List<User> user = userService.userByRole(roleLabel);
        return Result.success(user);
    }

    @GetMapping("/dept/{deptId}")
    public Result userByDept( @PathVariable Number deptId){
        List<User> user = userService.userByDept(deptId);
        return Result.success(user);
    }
}
